﻿Imports Microsoft.VisualBasic

Imports System.Data.SqlClient
Imports System.Data
Imports System.Collections.Generic

Namespace Pic
    Public Class Likes
        Inherits clsBase
#Region "Private Variables"
        Private _UserEmail As String
        Private _ObjectId As Integer
        Private _Created As DateTime
#End Region
#Region "Properties"
        Public Property UserEmail As String
            Set(value As String)
                _UserEmail = value
            End Set
            Get
                Return _UserEmail
            End Get
        End Property
        Public Property ObjectId As Integer
            Set(value As Integer)
                _ObjectId = value
            End Set
            Get
                Return _ObjectId
            End Get
        End Property
        Public Property Created As DateTime
            Set(value As DateTime)
                _Created = value
            End Set
            Get
                Return _Created
            End Get
        End Property

#End Region
#Region "Constructor"
        Public Sub New(ByVal UserEmail_ As Integer, ByVal ObjectId_ As Integer, ByVal Created_ As DateTime)
            Me.UserEmail = UserEmail_
            Me.ObjectId = ObjectId_
            Me.Created = Created_
        End Sub
#End Region
#Region "Methods"
        Private Shared Function GetLikesFromReader(ByVal reader_ As IDataReader) As Likes
            Dim rtrn As Likes = New Likes(reader_("UserEmail"), reader_("ObjectId"), reader_("Created"))
            Return rtrn
        End Function
        Public Shared Function GetLikeByUserEmailAndObjectId(ByVal UserEmail_ As String, ByVal ObjectId_ As Integer) As Likes
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "Select * from tbl_Likes where user_email=@UserEmail and object_id=@ObjectId"
            Dim reader As IDataReader
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text

                cmd.Parameters.Add("@UserEmail", SqlDbType.VarChar).Value = UserEmail_
                cmd.Parameters.Add("@ObjectId", SqlDbType.Int).Value = ObjectId_
                reader = ExecuteReader(cmd, CommandBehavior.SingleRow)
                If reader.Read Then
                    Return GetLikesFromReader(reader)
                Else
                    Return Nothing
                End If
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Private Shared Function GetLikesListFromReader(ByVal reader As IDataReader) As List(Of Likes)
            Dim rtrnList As New List(Of Likes)
            Do While reader.Read
                rtrnList.Add(GetLikesFromReader(reader))
            Loop
            Return rtrnList
        End Function
        Public Shared Function GetLikesListByFilter(ByVal Filtre_ As String, ByVal FiltreOrder_ As String) As List(Of Likes)
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "select * from website1.dbo.tbl_Likes where " & Filtre_ & "' order by " & FiltreOrder_ & " desc"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                Return GetLikesListFromReader(ExecuteReader(cmd, CommandBehavior.Default))
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function InsertLike(ByVal UserEmail_ As String, ByVal ObjectID_ As Integer) As Integer
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "insert into tbl_Likes(user_email,object_id,created) values(@UserEmail,@ObjectID,@created)"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@UserEmail", SqlDbType.VarChar).Value = UserEmail_
                cmd.Parameters.Add("@ObjectID", SqlDbType.Int).Value = ObjectID_
                cmd.Parameters.Add("@created", SqlDbType.DateTime).Value = Now.Date
                Return ExecuteNonQuery(cmd)
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function

        Public Shared Function UpdateLike(ByVal UserEmail_ As String, ByVal ObjectID_ As Integer) As Integer
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "update tbl_Likes set user_email=@UserEmail,object_id=@ObjectID where user_email=@UserEmail and  object_id=@ObjectID"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@UserEmail", SqlDbType.VarChar).Value = UserEmail_
                cmd.Parameters.Add("@ObjectID", SqlDbType.Int).Value = ObjectID_
                Return ExecuteNonQuery(cmd)
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function DeleteLike(ByVal UserEmail_ As String, ByVal ObjectID_ As Integer) As Boolean
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "Select count(1) from tbl_Likes where user_email=@UserEmail and object_id=@ObjectID"
            Dim count As Integer
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.Parameters.Add("@UserEmail", SqlDbType.VarChar).Value = UserEmail_
                cmd.Parameters.Add("@ObjectID", SqlDbType.Int).Value = ObjectID_
                count = ExecuteScalar(cmd)
                If count = 0 Then
                    Return False
                Else
                    cmd.CommandText = "Delete from tbl_Likes where user_email=@UserEmail and object_id=@ObjectID"
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.Add("@UserEmail", SqlDbType.VarChar).Value = UserEmail_
                    cmd.Parameters.Add("@ObjectID", SqlDbType.Int).Value = ObjectID_
                    Return ExecuteNonQuery(cmd)

                End If
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function

        'Public Shared Function SilinebilirMi(ByVal ProjeID_ As Integer) As Boolean
        '    Dim conn As New SqlConnection(ConnectionString)
        '    Dim veriString As String = "Select count(1) from tblSondajlar where ProjeID=" & ProjeID_
        '    Dim count As Integer
        '    Try
        '        conn.Open()
        '        Dim cmd As New SqlCommand(veriString, conn)
        '        count = ExecuteScalar(cmd)
        '        If count = 0 Then
        '            Return True
        '        Else
        '            Return False
        '        End If
        '    Catch ex As Exception
        '        Throw ex
        '    Finally
        '        conn.Close()
        '    End Try
        'End Function
#End Region

    End Class
End Namespace

